#include "stack.h"
//栈的初始化
void StackInit(Stack* s)
{
    s->arr = NULL;
    s->capacity = s->top = 0;
}
//入栈
void StackPush(Stack* s,STDateType x)
{
    assert(s);
    if(s->capacity == s->top)
    {
        int newCapacity = s->capacity == 0 ? 4 : 2 * s->capacity;
        STDateType* newArr = (STDateType*)realloc(s->arr,sizeof(STDateType)*newCapacity);
        if(newArr == NULL)
        {
            perror("realloc error!\n");
            exit(1);
        }
        s->arr = newArr;
        s->capacity = newCapacity;
    }
    *(s->arr + s->top) = x;
    s->top++;
}
//出栈
void StackPop(Stack* s)
{
    assert(s);
    s->top--;
}
//取栈顶元素
STDateType StackTop(Stack* s)
{
    assert(s && s->top != 0);
    return *(s->arr + s->top - 1);
}
//判断栈是否为空
bool StackEmpty(Stack* s)
{
    assert(s);
    return !s->top;
}
//栈的有效元素个数
int StackSize(Stack* s)
{
    assert(s);
    return s->top;
}
//栈的销毁
void StackDestroy(Stack* s)
{
    assert(s);
    if(s->arr != NULL)
    {
        free(s->arr);
        s->arr = NULL;
    }
    s->capacity = s->top = 0;
}
